import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

// 26033
public class P60 {
	static List<Integer> primeList = new ArrayList<Integer>();
	static Set<Integer> primeSet = new HashSet<Integer>(); 
	static Set<Integer> NotPrimeSet = new HashSet<Integer>();
	static Set<String> failSet = new HashSet<String>();

	private static boolean check(int x, int y) {
		int t = Integer.parseInt(x + "" + y);
		if (primeSet.contains(t)) {	
			return true;
		} 
		
		if (NotPrimeSet.contains(t)) {
			return false;
		} 
	
		if (Util.isPrime(t)) {
			primeSet.add(t);
			return true;
		}
		
		NotPrimeSet.add(t);
		return false;		
	}
	
	private static boolean check2(int x, int y) {
		if (!check(x, y)) {								
			return false;
		}
		
		if (!check(y, x)) {
			return false;
		}
		
		return true;
	}
	
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		for (int i = 2; true; i++) {
			System.out.println(i);
			
			if (Util.isPrime2(i)) {
				primeList.add(i);				
				primeSet.add(i);
				
				int n = 5;
				int size = primeList.size();
				if (size >= n) {
					
					int[] x = new int[n];
					for (int a = 0; a < size - 2; a++) {
						x[0] = primeList.get(a);
						x[4] = primeList.get(size - 1);
						
						if (!check2(x[0], x[4])) {
							continue;
						}	
						
						for (int b = a + 1; b < size - 2; b++) {
							x[1] = primeList.get(b);
							
							if (!check2(x[1], x[4])) {
								continue;
							}
							
							if (!check2(x[0], x[1])) {	
								continue;
							}
							
							for (int c = b + 1; c < size - 2; c++) {
								x[2] = primeList.get(c);
								
								if (!check2(x[2], x[4])) {
									continue;
								}
								
								if (!check2(x[1], x[2])) {	
									continue;
								}
								
								if (!check2(x[0], x[2])) {	
									continue;
								}								
								
								for (int d = c + 1; d < size - 2; d++) {
									x[3] = primeList.get(d);
									
									if (!check2(x[3], x[4])) {
										continue;
									}
									
									if (!check2(x[2], x[3])) {
										continue;
									}
									
									if (!check2(x[1], x[3])) {
										continue;
									}
									
									if (!check2(x[0], x[3])) {	
										continue;
									}								
															
									int sum = 0;
									for (int k = 0; k < x.length; k++) {
										sum += x[k];
									}
									
									System.out.println(Arrays.toString(x));
									System.out.println(sum);
									System.out.println(System.currentTimeMillis() - startTime);
									return;
								}
							}
						}
					}
				}
			}						
		}
	}
}
